package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendGoOutRuleAddDTO;
import org.jsola.hr.dto.AttendGoOutRuleUpdateDTO;
import org.jsola.hr.query.AttendGoOutRuleQuery;
import org.jsola.hr.service.IAttendGoOutRuleService;
import org.jsola.hr.vo.AttendGoOutRuleListVO;
import org.jsola.hr.vo.AttendGoOutRuleVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrAttendGoOutRuleController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendGoOutRules")
public class AttendGoOutRuleController implements IAttendGoOutRuleControllerAPI {

    @Autowired
    private IAttendGoOutRuleService attendGoOutRuleService;

    @Override
    @PostMapping
    public Result<AttendGoOutRuleVO> save(@Valid @RequestBody AttendGoOutRuleAddDTO attendGoOutRuleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增外出打卡规则权限校验

        return Result.success(
                attendGoOutRuleService.save(attendGoOutRuleAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendGoOutRuleUpdateDTO attendGoOutRuleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改外出打卡规则权限校验

        int count = attendGoOutRuleService.update(attendGoOutRuleUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendGoOutRuleId}")
    public Result<Integer> delete(@PathVariable String attendGoOutRuleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除外出打卡规则权限校验

        int count = attendGoOutRuleService.deleteByIds(tokenUser, attendGoOutRuleId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendGoOutRuleIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除外出打卡规则权限校验

        int count = attendGoOutRuleService.deleteByIds(tokenUser, attendGoOutRuleIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendGoOutRuleId}")
    public Result<AttendGoOutRuleVO> findById(@PathVariable String attendGoOutRuleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看外出打卡规则权限校验

        return Result.success(
            attendGoOutRuleService.selectById(attendGoOutRuleId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendGoOutRuleListVO>> listAll(@Valid AttendGoOutRuleQuery attendGoOutRuleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看外出打卡规则权限校验

        return Result.success(
            attendGoOutRuleService.select(attendGoOutRuleQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendGoOutRuleListVO>> page(@Valid AttendGoOutRuleQuery attendGoOutRuleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendGoOutRuleService.selectPage(attendGoOutRuleQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 查询外出打卡规则
     * @return
     */
    @Override
    @GetMapping(value = "findAttendGoOutRule/{companyId}")
    public Result<AttendGoOutRuleVO> findAttendGoOutRule(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendGoOutRuleService.selectOneByCompanyId(companyId, tokenUser.getSiteId())
        );
    }


}

